package com.study.d4_thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Test3 {
    public static void main(String[] args) throws Exception{
        //目标：掌握多线程创建方式三：可以返回线程执行完毕后的结果
        Callable<String> call = new MyCallable(100);
        FutureTask<String> task = new FutureTask<>(call);
        Thread t = new Thread(task);
        t.start();
        System.out.println(task.get());
    }

}
class MyCallable implements Callable<String>{
    private int n;
    public MyCallable(int n){
        this.n=n;
    }
    @Override
    public String call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return "子线程求和1-"+n+"的结果："+sum;
    }
}
